package com.bigdata.assignment.problem2;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * 按单词长度分区
 * 规则：
 * - 长度 0-2 → partition 0
 * - 长度 3-5 → partition 1
 * - 长度 6-8 → partition 2
 * - 长度 ≥9 → partition 3
 */
public class LengthPartitioner extends Partitioner<Text, IntWritable> {

    @Override
    public int getPartition(Text key, IntWritable value, int numPartitions) {
        if (numPartitions == 0) return 0;
        if (key == null) return 0;

        int length = key.toString().length();
        int partition;

        if (length <= 2) {
            partition = 0;
        } else if (length <= 5) {
            partition = 1;
        } else if (length <= 8) {
            partition = 2;
        } else {
            partition = 3;
        }

        return partition % numPartitions;
    }
}